package controllers

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"io/ioutil"
	"net/http"
	"os"
	"strconv"
)

func CheckNewVer(c *gin.Context){

	appName := c.PostForm("appName")
	appName = `C:\Users\Administrator\go\src\ScriptServer\test\`+appName
	currVer,err := strconv.ParseFloat(c.PostForm("currVer"),64)
	fmt.Println(os.Args[0])
	if err != nil {
		c.AbortWithStatusJSON(http.StatusOK,gin.H{"success":false,"message":"输入错误"})
		return
	}
	var newVer float64
	_,err =os.Stat(appName)
	if os.IsNotExist(err){
		c.AbortWithStatusJSON(http.StatusOK,gin.H{"success":false,"message":"错误的应用"})
		return
	}
	files,err := ioutil.ReadDir(appName)
	for _,info := range files {
		if !info.IsDir() {
			ver, err := strconv.ParseFloat(info.Name(), 64)
			if err != nil {
				continue
			}
			if ver > currVer {
				newVer = ver
			}
		}
	}
	if newVer != 0 {
		c.JSON(http.StatusOK,gin.H{"success":true,"message":newVer})
		return
	}
	c.AbortWithStatusJSON(http.StatusOK,gin.H{"success":false,"message":"错误的应用"})
}

//func DownloadNewVer(c *gin.Context) {
//	appName := c.Param("appName")
//	ver := c.Param("ver")
//
//}